﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WXYW.BLL;
using WXYW.DAL;
using WXYW.Model;

namespace WXYW
{
    public partial class SparePartLendRecord : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindLendRecordsGridView();
            }
        }

        private void BindLendRecordsGridView()
        {
            SparePartService sparePartService = new SparePartService();
            LendRecordGridView.DataSource = sparePartService.GetSparePartLendRecordsModel(ContextService.CurrentAccount, keyWordTextBox.Text);
            LendRecordGridView.DataBind();
        }

        protected void LendRecordGridView_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName.Equals("Back"))
            {
                SparePartService sparePartService = new SparePartService();
                WXYW.DAL.SparePartLendRecord lendRecord = sparePartService.GetLendRecordByID(Convert.ToInt32(e.CommandArgument));
                HiddenField.Value = e.CommandArgument.ToString();
                modelNumberTxtBox.Text = lendRecord.ModelNumber;
                versionNumberTxtBox.Text = lendRecord.VersionNumber;
                lendCountTxtBox.Text = lendRecord.LendCount.ToString();
                lendDateTxtBox.Text = lendRecord.LendDate.ToShortDateString();
                backDateTxtBox.Text = DateTime.Now.ToShortDateString();
                returnComment.Text = "";
                warningInfo.Text = "";
                BackPopupExtender.Show();
            }
        }

        protected void backButton_Click(object sender, EventArgs e)
        {
            SparePartService sparePartService = new SparePartService();
            try
            {
                sparePartService.BackSparePart(Convert.ToInt32(HiddenField.Value), ContextService.CurrentAccount, modelNumberTxtBox.Text,
                    versionNumberTxtBox.Text, Convert.ToInt32(lendCountTxtBox.Text), returnComment.Text);
                BindLendRecordsGridView();
            }
            catch (Exception ex)
            {
                warningInfo.Text = ex.Message;
                BackPopupExtender.Show();
            }
        }

        protected void LendRecordGridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow &&
                ((SparePartLendRecordModel)e.Row.DataItem).Status.Equals("已归还"))
            {
                ((LinkButton)e.Row.FindControl("backLinkButton")).Visible = false;
            }

            if (e.Row.DataItem != null)
            {
                e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#FFFF99'");
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
                ((Label)e.Row.FindControl("IDLbl")).Text = (e.Row.RowIndex + 1).ToString();
            }
        }

        protected void searchLinkButton_Click(object sender, EventArgs e)
        {
            BindLendRecordsGridView();
        }

        protected void resetLinkButton_Click(object sender, EventArgs e)
        {
            keyWordTextBox.Text = "";
            BindLendRecordsGridView();
        }

        protected void LendRecordGridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            LendRecordGridView.PageIndex = e.NewPageIndex;
            BindLendRecordsGridView();
        }
    }
}
